package top.roadvast.springboot.log.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import top.roadvast.springboot.log.bean.LogObject;

/**
 * @Author 郭泽鹏
 * @Date 2021/10/10 0:16
 * @Version 1.0
 * @Description
 */
@Slf4j
@Component(value = "logTemplate")
public class LogTemplate {

    public LogObject setReqLog(String methodName, String traceId, Integer backendId, Object request) {
        LogObject logObject = new LogObject();
        return logObject.setEventName(methodName)
                .setTraceId(traceId)
                .setUserId(backendId)
                .setRequest(request);
    }

    public void setLog(String methodName, String msg, String traceId, Integer backendId, Object request,
                       Object response, long beginTime) {
        long endTime = System.currentTimeMillis();
        LogObject logObject = new LogObject();
        logObject.setEventName(methodName)
                .setMsg(msg)
                .setTraceId(traceId)
                .setUserId(backendId)
                .setRequest(request)
                .setResponse(response)
                .setCostTime((endTime - beginTime));
        log.info(logObject.toString());
    }

}
